package dS.ProjectGPS.LeicaOWI;

import java.util.Vector;
import javax.microedition.lcdui.*;

public class FormOwiOPC extends Form implements FormOwi {
  OWIAgent owiAgent = null;

  ChoiceGroup contentFlag = new ChoiceGroup("Content Flag", ChoiceGroup.POPUP);
  ChoiceGroup processing = new ChoiceGroup("Processing", ChoiceGroup.POPUP);
  ChoiceGroup dopplerValues = new ChoiceGroup("Doppler Values",
                                              ChoiceGroup.POPUP);

  public FormOwiOPC(OWIAgent o) {
    super("Observation Processing");

    owiAgent = o;

    try {
      ndsInit();
    }
    catch (Exception ex) {
    }
  }

  public void ndsInit() throws Exception {
    append(contentFlag);
    contentFlag.append("Nothing Available", null);
    contentFlag.append("Processing Block Included", null);
    contentFlag.append("Doppler Values Block Included", null);
    contentFlag.append("Processing and Doppler Block Included", null);
    append(processing);
    append(dopplerValues);
    processing.append("Sampled", null);
    processing.append("Smoothed", null);
    dopplerValues.append("Not Included", null);
    dopplerValues.append("Included", null);
  }

  public void update() {
    owiAgent.query(OWI.qsOPC());

    contentFlag.setSelectedIndex(OWI.opcMsgContent, true);
    processing.setSelectedIndex(OWI.opcProcessing, true);
    dopplerValues.setSelectedIndex(OWI.opcDopplerValues, true);
  }

  public void save() {

    String msgId = "OPC";

    Vector outputs = new Vector();
    // Setup SET parameters
    outputs.addElement(msgId);

    if(contentFlag.getSelectedIndex() == 1){
      outputs.addElement("" + processing.getSelectedIndex());

    } else if (contentFlag.getSelectedIndex() == 2){
      outputs.addElement("" + dopplerValues.getSelectedIndex());

    } else if (contentFlag.getSelectedIndex() == 3){
      outputs.addElement("" + processing.getSelectedIndex());
      outputs.addElement("" + dopplerValues.getSelectedIndex());
    }


    String[] outputSA = new String[outputs.size()];
    for(int i=0; i<outputs.size(); i++){
      outputSA[i] = (String) outputs.elementAt(i);
    }

    //Send message and update form if successful
    int ackCode = (owiAgent.set(msgId, OWI.setMessage(outputSA)));
    Controller.showACKAlert(this, this, ackCode);
  }


}
